package com.timerecording.service.newSystem;

import com.timerecording.Enum.DeleteEnum;
import com.timerecording.dao.OperateDao;
import com.timerecording.dao.newSystem.NewRecordTimeDao;
import com.timerecording.domain.entity.OperateLogEntity;
import com.timerecording.domain.entity.newSystem.NewRecordTimeEntity;
import com.timerecording.domain.form.NewRecordTimeAddForm;
import com.timerecording.domain.vo.newSystem.NewRecordTimeVO;
import com.timerecording.util.R;
import com.timerecording.util.ResponseCodeConst;
import com.timerecording.util.ResponseDTO;
import com.timerecording.util.SmartBeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author smz41
 * @description: TODO
 * @date 2024/8/4
 */

@Service
public class NewRecordTimeService {


    @Resource
    private NewRecordTimeDao newRecordTimeDao;

    @Resource
    private OperateDao operateDao;

    /**
     * sqt 新增工时
     *
     * @param newRecordTimeAddForm
     * @return
     * @throws UnknownHostException
     */
    public R insertNewRecordTime(NewRecordTimeAddForm newRecordTimeAddForm) throws UnknownHostException {
        NewRecordTimeEntity newRecordTimeEntity = SmartBeanUtil.copy(newRecordTimeAddForm, NewRecordTimeEntity.class);
        //日志记录
        OperateLogEntity operateLogEntity = new OperateLogEntity();
        operateLogEntity.setCreateTime(new Date());
        operateLogEntity.setSuccessFlag(Boolean.TRUE);
        operateLogEntity.setUpdateTime(new Date());
        operateLogEntity.setUrl("/new-record/addNewRecordTime");
        InetAddress addr = InetAddress.getLocalHost();
        operateLogEntity.setIp(addr.toString());
        if (null == newRecordTimeAddForm.getRecordId()) {
            newRecordTimeEntity.setDeleteFlag(Boolean.FALSE);
            newRecordTimeEntity.setCreateTime(new Date());
            newRecordTimeEntity.setUpdateTime(new Date());
            newRecordTimeDao.insert(newRecordTimeEntity);
            //新增日志记录
            operateLogEntity.setContent("新增工时记录 @author smz");
            operateDao.insert(operateLogEntity);
            return R.ok();
        }
        //修改工时记录
        operateLogEntity.setContent("修改工时记录 @author smz");
        newRecordTimeDao.updateById(newRecordTimeEntity);
        operateDao.insert(operateLogEntity);
        return R.ok();

    }

    /**
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/8/4
     */

    public ResponseDTO<List<NewRecordTimeVO>> queryNewRecordTime(String recordStartDate, String recordEndDate, String toDayTime) {
        List<NewRecordTimeVO> list = newRecordTimeDao.queryNewRecordTime(recordStartDate, recordEndDate, toDayTime, DeleteEnum.NORMAL.getValue());
        return ResponseDTO.succData(list, "查询成功");
    }

    /**
     * 逻辑删除工时记录  修改状态为删除
     *
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/9/8
     */

    public void deleteNewRecordTime(Integer recordId) throws UnknownHostException {
        if (StringUtils.isNotBlank(String.valueOf(recordId))) {
            newRecordTimeDao.updateDeleteById(recordId);
            //日志记录
            OperateLogEntity operateLogEntity = new OperateLogEntity();
            operateLogEntity.setCreateTime(new Date());
            operateLogEntity.setSuccessFlag(Boolean.TRUE);
            operateLogEntity.setUpdateTime(new Date());
            operateLogEntity.setUrl("/new-record/deleteNewRecordTime");
            InetAddress addr = InetAddress.getLocalHost();
            operateLogEntity.setIp(addr.toString());
            //删除日志记录
            operateLogEntity.setContent("工时记录表逻辑删除记录 @author smz");
            operateDao.insert(operateLogEntity);
        }

    }

    /**
     * 逻辑删除工时记录  修改状态为正常
     *
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/9/8
     */

    public void recoverSalaryById(Integer recordId) throws UnknownHostException {
        if (StringUtils.isNotBlank(String.valueOf(recordId))) {
            newRecordTimeDao.recoverSalaryById(recordId);
            //日志记录
            OperateLogEntity operateLogEntity = new OperateLogEntity();
            operateLogEntity.setCreateTime(new Date());
            operateLogEntity.setSuccessFlag(Boolean.TRUE);
            operateLogEntity.setUpdateTime(new Date());
            operateLogEntity.setUrl("/new-record/recoverSalaryById");
            InetAddress addr = InetAddress.getLocalHost();
            operateLogEntity.setIp(addr.toString());
            //删除日志记录
            operateLogEntity.setContent("工时记录表逻辑删除记录 @author smz");
            operateDao.insert(operateLogEntity);
        }

    }

    /**
     * 查询工时详情
     *
     * @description: TODO
     * @author smz
     * @version 1.0
     * @date 2024/9/16
     */

    public ResponseDTO<NewRecordTimeEntity> newInfoRecordTime(Integer recordId) {
        //创建一个空的对象，用于接受查询的数据
        NewRecordTimeEntity recordTimeEntityCopy = new NewRecordTimeEntity();
        //进行数据库操作
        NewRecordTimeEntity newRecordTimeEntity = newRecordTimeDao.selectRecordTimeInfo(recordId);
        if (!Objects.isNull(newRecordTimeEntity)) {
            SmartBeanUtil.cpoyProperties(newRecordTimeEntity, recordTimeEntityCopy);
        }
        return ResponseDTO.succData(recordTimeEntityCopy, "查询成功！");
    }
}

